function latexTableSimple(fid,mat,rowNames,colNames,rowTextIndic,cornerName,caption)
%==========================================================================
% latexTable(fid,mat,rowNames,colNames,cornerName,caption)
% 
% This writes a simple Latex table into an existing document using the 
% Longtable environment 
% 
% Inputs 
% fid:          File identifier 
% mat:                  [row col] matrix 
% rowNames:             [row   1] cell with names of the rows 
% colNames:             [col   1] cell with names of the columns 
% rowcolTextIndic       [1     1] or [row 1] vector equal 
%                       to 1 if all rows or the j-th row should be a
%                       string, and hence, not have $ $ appended
% cornerName:           [1     1] optional string to go on the left corner of the
%                       table 
% caption:              [1     1] optional caption at the bottom of the table 
%
% Alejandro Justiniano Sep 4 2012 
%==========================================================================

%% 1. Dimensions 
[Nrow Ncol]=size(mat); 
if length(rowNames)~=Nrow 
    error('Wrong Dimension of Row Names'); 
end 
if length(colNames)~=Ncol 
    error('Wrong Dimension Column Names'); 
end 

if any(rowTextIndic) < 0 && any(rowTextIndic) > 1; 
    error('rowTextIndic must be 0 1') 
end 
if length(rowTextIndic) > 1 
    if length(rowTextIndic) < Nrow 
    error('rowTextIndic is either [1,1] or [nrow,1]') 
    end 
else 
    rowTextIndic=rowTextIndic*ones(Nrow,1); 
end    

%% 2. Strings that determine the formatting and corner 
if isempty(cornerName)==false;
    stringHeader=char(cornerName);
else
    stringHeader='';
end
stringTop='|r'; 
for ii=1:Ncol; 
    stringTop=strcat(stringTop,'|c|');
    stringHeader=strcat(stringHeader,'&',char(colNames(ii)));
end 
%% 3. Begin Table and Headers
fprintf(fid,'\\begin{center} \n'); 
fprintf(fid,['\\begin{longtable}{',stringTop,'} \\hline\\hline \n']); 
fprintf(fid,[stringHeader, '\\\\ \\hline \n']); 

%% 4. Write rows of Matrix 
for ii=1:Nrow; 
    if rowTextIndic(ii)==1
        str=char(rowNames(ii));
        %str=regexprep(str,'_',''); 
        %str=regexprep(str,'^',''); 
        %str=regexprep(str,'\\',''); 
    else
        str=strcat('$',char(rowNames(ii)),'$');
    end
    for jj=1:Ncol;
        str=strcat(str,'&',sprintf('%4.2f',mat(ii,jj)));
    end 
    fprintf(fid,[str,' \\\\ \\hline \n']); 
end 
if isempty(caption)==false
    fprintf(fid,['\\caption{',char(caption),'} \n']);
end

%% 3. End Table
fprintf(fid,'\\end{longtable} \n');
fprintf(fid,'\\end{center} \n');
fprintf(fid,'\n');

%fprintf(fid,'\\end{document} \n');
%fclose(fid);